package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.app.Activity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsInterstitialAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsVideoPlayConfig;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;

import java.util.List;

public class KsLoadInterstitialAd extends AdBaseLoad implements AdLoad {
    private volatile static KsLoadInterstitialAd mInstance;
    KsInterstitialAd mKsInterstitialAd;

    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadInterstitialAd getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadInterstitialAd.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadInterstitialAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(Activity activity, FrameLayout frameLayout) {
        mKsInterstitialAd = null;
        if (KsAdId==null&& AdCode.getKsLoadInterstitialId()==0){
            Log.e(TAG,"KsLoadInterstitialAd————————还未设置ID:");
            return;
        }
        if (ToastCode){
            Log.e(TAG,KsAdId+"mmmmmmmmmmm");
            Toast.makeText(activity,KsAdId!=null ? KsAdId+"": AdCode.getKsLoadInterstitialId()+"",Toast.LENGTH_SHORT).show();
        }
        KsScene scene = new KsScene.Builder(KsAdId!=null ? KsAdId:AdCode.getKsLoadInterstitialId()).build();
        KsAdSDK.getLoadManager().loadInterstitialAd(scene, new KsLoadManager.InterstitialAdListener() {
            @Override
            public void onError(int code, String msg) {
                Log.e(TAG,code+msg);
                if (listener!=null){
                    listener.onError(code+msg);
                }
            }
            @Override
            public void onRequestResult(int adNumber) {
                Log.e(TAG,"告请求填充个数");
            }
            @Override
            public void onInterstitialAdLoad(@Nullable List<KsInterstitialAd> adList) {
                if (adList != null && adList.size() > 0) {
                    mKsInterstitialAd = adList.get(0);
                    Log.e(TAG,"插屏⼴告请求成功");
                    KsVideoPlayConfig videoPlayConfig = new
                            KsVideoPlayConfig.Builder()
                            .build();
                    // showInterstitialAd(videoPlayConfig);
                    if (mKsInterstitialAd != null) {
                        mKsInterstitialAd.setAdInteractionListener(new KsInterstitialAd.AdInteractionListener() {
                            @Override
                            public void onAdClicked() {
                                Log.e(TAG,"插屏⼴插屏广告点击");
                                if (listener!=null){
                                    listener.onAdClicked();
                                }
                            }

                            @Override
                            public void onAdShow() {
                                Log.e(TAG,"插屏广告曝光");
                                if (listener!=null){
                                    listener.onAdShow();
                                }
                            }

                            @Override
                            public void onAdClosed() {
                                Log.e(TAG,"用户点击插屏关闭按钮");
                            }

                            @Override
                            public void onPageDismiss() {
                                Log.e(TAG,"插屏广告关闭");
                                if (listener!=null){
                                    listener.onPageDismiss();
                                }
                            }

                            @Override
                            public void onVideoPlayError(int code, int extra) {
                                Log.e(TAG,"插屏广告播放出错");

                                if (listener!=null){
                                    listener.onError(code+"");
                                }
                            }

                            @Override
                            public void onVideoPlayEnd() {
                                Log.e(TAG,"插屏广告播放完成");
                                if (listener!=null){
                                    listener.onComplete();
                                }
                            }

                            @Override
                            public void onVideoPlayStart() {
                                Log.e(TAG,"插屏⼴插屏广告播放开始");
                            }

                            @Override
                            public void onSkippedAd() {
                                Log.e(TAG,"插屏⼴插屏广告播放跳过");
                            }

                        });

                        mKsInterstitialAd.showInterstitialAd(activity, videoPlayConfig);
                    } else {
                        Log.e(TAG,"插屏⼴暂无可用插屏广告，请等待缓存加载或者重新刷新");
                    }
                }
            }
        });
    }


}
